from flask import Blueprint,request
from flaskDemo.sql.mysql_db import mysql_db
from flaskDemo.config.config import Config
from flaskDemo.extensions.success import list_result
from flaskDemo.sql.sqlModel import SqlModel
from flask_socketio import SocketIO, emit,send

message = Blueprint('message', __name__)
err = Config.ERR

@message.route("/getMessageList",methods=['GET'])
def getMessageList():
    params = request.values
    id = params.get("id")
    with mysql_db() as db:
        sql = f"select * from system_message where receive_user = '{id}'"
        result = db.fetch_all(sql)
        return list_result(result)

def addMessage(params):
    # with mysql_db() as db:
    #     model = SqlModel("system_message", [params])
    #     sql = model.insert_sql()
    #     db.insert(sql)
    #     db.conn.commit()
    emit("socketTest2", params, room="5fd955d8-1b")

@message.route("/updIsRead",methods=["POST"])
def updIsRead():
    params = request.json
    id = params.get("id")
    with mysql_db() as db:
        if id is not None:
            model = SqlModel("system_message", [params])
            sql = model.update_sql()
            sql = f"{sql} where id = '{id}'"
            db.update(sql)
            db.conn.commit()
            return list_result()


if __name__ == "__main__":
    addMessage({"message":1})